<?php
/**
 * @package Bike
 * @subpackage Session
 */
/**
 * Class for handling sessions
 * @author Chrome
 * @package Bike
 * @subpackage Session
 */
class Bike_Session {

	/**
	 * Session name
	 * @var string
	 */
	protected $name = '';

	/**
	 * Indicate is session started
	 * @var boolean
	 */
	protected $started = false;

	
	/**
	 * Constructor
	 * @param string $name
	 */
	public function __construct($name){
		$this->name = $name;
	}

	/**
	 * Stode $value by $name key
	 * @param string $name
	 * @param mixed $value
	 * @return boolean
	 */
	public function set($name, $value){
		if($this->started){
			$_SESSION[$name] = $value;
			return true;
		}
		return false;
	}

	/**
	 * Get value by $name
	 * @param string $name
	 * @return mixed
	 */
	public function get($name){
		if($this->started && !empty($_SESSION[$name])){
			return $_SESSION[$name];
		}else{
			return null;
		}
	}

	/**
	 * Starts session
	 * @return boolean
	 */
	public function begin(){
		if(!$this->started){
			session_name($this->name);
			session_start();
			$this->started = true;
		}
		return $this->started;
	}

	/**
	 * Destroys session
	 * @return boolean
	 */
	public function end(){
		if($this->started){
			$_SESSION = array();
			session_destroy();
			$this->started = false;
		}
		return true;
	}

	/**
	 * Check if session is started
	 * @return boolean
	 */
	public function isStarted(){
		return $this->started;
	}

	/**
	 * Get session name
	 * @return string
	 */
	public function getName(){
		return $this->name;
	}
}
